<?php
namespace control;
/*
*银行账户余额表类
*/

class accountBalanceClass extends adIndexClass
{
	private const page  = 'accountBalance';
	private const title = '银行账户余额表';
	
	/*列表-查询条件*/
	public function home()
	{
		//生成查询表单
		$html =
			text(array("name" => "code", "title" => "账户编码")) .
			text(array("name" => "name", "title" => "账户名称")) .
			text(array("name" => "orderNo", "title" => "单据编号")) .
			text(array("name" => "targetName", "title" => "往来单位")) .
			text(array("name" => "stName", "title" => "经手人")) .
			text(array("name" => "text", "title" => "备注")) .
			select(["name" => "type", 'option' => ['收入', '支出'], 'title' => '业务类型']) .
			text(array("name" => "createTime", 'class' => 'width200', 'laydate' => ['type' => 'date', 'range' => '~'], "title" => "单据日期-单据日期"));
		//返回
		return $this->homeFun($this->search($html));
	}
	
	/*客户列表-查询结果*/
	public function homeSearch()
	{
		//like查询数组
		$like = array(
			'b.orderNo'    => $this->post('code'),
			'b.name'       => $this->post('name'),
			'a.orderNo'    => $this->post('orderNo'),
			'a.targetName' => $this->post('targetName'),
			'a.stName'     => $this->post('stName'),
			'a.text'       => $this->post('text'),
		);
		//全等查询数组
		$equal = array(
			'a.coid' => self::$coid,
		);
		$and   = match ($this->post('type')) {
			'收入'    => " and direction =1",
			'支出'    => " and direction =0",
			default => '',
		};
		if ($this->post('createTime')) {
			$and .= self::timeComparison($this->post('createTime'), 'date', 'targetDate');
		}
		//获取查询结果
		$where                       = search(array("like" => $like, "=" => $equal)) . $and;
		$page                        = $this->page(array("table" => "account_record as a join account as b on a.acid = b.id", 'field' => "a.*,b.orderNo as code,b.name", "where" => $where, "order" => "a.id desc"));
		$_SESSION[self::page]['sql'] = $page['fullSql'];
		//获取表头
		$tr[] = array('账户编号', '账户名称', "单据日期", '单据编号', "业务类型", '收入(' . static::$unit . ')', '支出(' . static::$unit . ')', '账户余额', '往来单位', '经手人', '备注');
		//获取记录
		foreach ($page['array'] as $array) {
			if ($array['direction'] == 1) {
				//收入
				$out = 0;
				$in  = $array['money'];
			} else {
				$out = $array['money'];
				$in  = 0;
			}
			$tr[] = array($array['code'], $array['name'], $array['targetDate'], $array['orderNo'], $array['direction'] == 1 ? '收入' : '支出', $in, $out, $array['balance'], $array['targetName'], $array['stName'], $array['text']);
		}
		//控件
		$button = "";
		if ($this->power(self::page, "export")) {
			$button .= "  <a href='" . root . "control/adIndex.html?cla=" . $this->get('cla') . "&fun=exportData'><span class='spanButton'>导出</span></a>\n";
		}
		//返回
		$json['html'] = $page['hint'] . $button . "<form name='listForm'>" . space . $this->tableMany(array("tr" => $tr)) . "</form>" . $page['button'] . clear;
		return json_encode($json, 320);
	}
	
	
	/**
	 * 导出
	 */
	public function exportData()
	{
		if (!$this->power(self::page, 'export')) {
			$_SESSION['warn'] = '你还没有导出的权限';
		} else {
			$tr     = [];
			$sql    = $_SESSION[self::page]['sql'];
			$title  = array('账户编号', '账户名称', "单据日期", '单据编号', "业务类型", '收入(' . static::$unit . ')', '支出(' . static::$unit . ')', '账户余额', '往来单位', '经手人');
			$result = sqlFind($sql);
			if (is_array($result)) {
				foreach ($result as $array) {
					if ($array['direction'] == 1) {
						//收入
						$out = 0;
						$in  = $array['money'];
					} else {
						$out = $array['money'];
						$in  = 0;
					}
					$tr[] = array($array['code'], $array['name'], $array['targetDate'], $array['orderNo'], $array['direction'] == 1 ? '收入' : '支出', $in, $out, $array['balance'], $array['targetName'], $array['stName']);
				}
			}
			$text = $this->control['name'] . "导出了银行账户余额表";
			$this->logText(["type" => self::page, "text" => $text]);
			$this->excelOut($title, $tr, "银行账户余额表.xlsx");
		}
		header("Location:" . getenv("HTTP_REFERER"));
		exit(0);
	}
}

